﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Utility.Helper
{
    public class LoadBalancerHelper
    {
        private readonly List<HttpClient> _httpClients;
        private readonly Random _random = new Random();

        public LoadBalancerHelper(IHttpClientFactory httpClientFactory)
        {
            _httpClients = new List<HttpClient>
            {
                httpClientFactory.CreateClient("ServiceInstance1"), // 实例 1
                httpClientFactory.CreateClient("ServiceInstance2"), // 实例 2
                httpClientFactory.CreateClient("ServiceInstance3")  // 实例 3
            };
        }

        public async Task<string> GetDataAsync()
        {
            // 随机选择一个 HttpClient 实例
            HttpClient client = _httpClients[_random.Next(_httpClients.Count)];
            HttpResponseMessage response = await client.GetAsync("data");
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
    }
}
